package com.cainaer.api.controller;

import com.cainaer.common.core.airecognition.Sample;
import com.cainaer.common.core.airecognition.utils.GsonUtils;
import com.cainaer.common.core.airecognition.domain.BaiduBankCardRes;
import com.cainaer.common.core.airecognition.domain.BusinessLicense;
import com.cainaer.common.core.airecognition.domain.IdCard;
import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.web.domain.AjaxResult;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


/**
 * @author-> tina
 * @date-> 2020/9/1 下午3:31
 * @version-> 1.0
 * @description-> 识别身份证号码
 **/
@RestController
@RequestMapping("/")
public class ImgController {


    @Value("${pathFile.img}")
    private String pathFileImg;

    @Value("${fileTomcat.imgUrl}")
    private String fileTomcatImgUrl;

    private String success = "识别成功";
    private String failure = "识别失败";

    private Logger logger = LoggerFactory.getLogger(ImgController.class);

    /**
     * 身份证正面面识别
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("front/{shopId}")
    public AjaxResult front(@RequestParam("file") MultipartFile file, Long shopId) throws IOException {
        logger.info("pathFile->{}", pathFileImg);
        if (!file.isEmpty()) {
            String furl = fileTomcatImgUrl + "idAcrd/" + shopId + "/front/";
            String fImg = pathFileImg + "idAcrd/" + shopId + "/front/";
            // Get the file and save it somewhere
            String myFileOriginalFilename = file.getOriginalFilename();
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(fImg, myFileOriginalFilename));
            IdCard idCard = Sample.front(fImg + myFileOriginalFilename);
            logger.info("idCard->{}", idCard);
            if (!StringUtils.isNull(idCard)) {
                idCard.setIdCardImg(furl + myFileOriginalFilename);
                return AjaxResult.success(success, idCard);
            }
        }
        return AjaxResult.error(failure);
    }

    /**
     * 身份证反面识别
     *
     * @param file
     * @return
     * @throws IOException IdCard idCard = Sample.back(pathFile + "idAcrd/" + shopId + "/" + myFileOriginalFilename);
     *                     if (StringUtils.isNotEmpty(idCard.toString())) {
     *                     return AjaxResult.success(success, idCard);
     *                     }
     *                     <p>
     *                     反面不做识别，需要需要就加入次代码
     */
    @PostMapping("back/{shopId}")
    public AjaxResult back(@RequestParam("file") MultipartFile file, Long shopId) throws IOException {
        logger.info("pathFile->{}", pathFileImg);
        if (!file.isEmpty()) {
            String burl = fileTomcatImgUrl + "idAcrd/" + shopId + "/back/";
            String bImg = pathFileImg + "idAcrd/" + shopId + "/back/";
            // 获取文件并将其保存在某处
            String myFileOriginalFilename = file.getOriginalFilename();
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(bImg, myFileOriginalFilename));
            IdCard back = Sample.back(bImg + myFileOriginalFilename);
            back.setIdCardImg(burl + myFileOriginalFilename);
            return AjaxResult.success(success, back);

        }
        return AjaxResult.error();
    }

    /**
     * 银行卡识别
     *
     * @param file
     * @return
     */
    @PostMapping("bankCard/{shopId}")
    public BaiduBankCardRes bankCard(@RequestParam("file") MultipartFile file, Long shopId) throws IOException {
        logger.info("pathFile->{}", pathFileImg);
        if (!file.isEmpty()) {
            String burl = fileTomcatImgUrl + "bankCard/" + shopId + "/";
            String bImg = pathFileImg + "bankCard/" + shopId + "/";
            // 获取文件并将其保存在某处
            String myFileOriginalFilename = file.getOriginalFilename();
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(bImg, myFileOriginalFilename));

            String jsonObject = Sample.bankCard((bImg + myFileOriginalFilename));
            BaiduBankCardRes baiduBankCardRes = GsonUtils.fromJson(jsonObject, BaiduBankCardRes.class);
            baiduBankCardRes.getResult().setBankCardImg(burl + myFileOriginalFilename);
            System.out.println("baiduBankCardRes->" + baiduBankCardRes);
            if (!StringUtils.isEmpty(jsonObject)) {
                return baiduBankCardRes;
            }

        }
        return null;
    }

    /**
     * 营业执照识别
     *
     * @param file
     * @return
     */
    @PostMapping("businessLicense/{shopId}")
    public AjaxResult businessLicense(@RequestParam("file") MultipartFile file, Long shopId) throws IOException {
        logger.info("pathFile->{}", pathFileImg);
        if (!file.isEmpty()) {
            String burl = fileTomcatImgUrl + "businessLicense/" + shopId + "/";
            String bImg = pathFileImg + "businessLicense/" + shopId + "/";
            // 获取文件并将其保存在某处
            String myFileOriginalFilename = file.getOriginalFilename();
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(bImg, myFileOriginalFilename));

            BusinessLicense businessLicense = Sample.businessLicense((bImg + myFileOriginalFilename));
            if (!StringUtils.isNull(businessLicense)) {
                businessLicense.setBusinessImg(burl + myFileOriginalFilename);
                return AjaxResult.success(success, businessLicense);
            }
        }
        return AjaxResult.error(failure);
    }
}